import { login, logout } from '@/api/login'
import { getPreset } from '@/api/table'
import { EMAILKey, getCookies, NICKNAMEKey, removeCookies, setCookies, TokenKey,
  UIDKey, BUSINESS, ROLESELF, ROLELEVEL, businessName, ISBACKCHOOSE ,setVcode} from '@/utils/auth'

const user = {
  state: {
    email: getCookies(EMAILKey),
    user_id: getCookies(UIDKey),
    token: getCookies(TokenKey),
    role: {
      'user_level': 0,
      'biz_permission': {
        'loki': 0
      }
    },
    roleSelf: getCookies(ROLESELF),
    business: getCookies(BUSINESS),
    businessName: getCookies(businessName),
    nickname: getCookies(NICKNAMEKey),
    menuList: JSON.parse(getCookies('menuList') || '[]'),
    roleLevel: getCookies(ROLELEVEL),
    isBackChoose: getCookies(ISBACKCHOOSE)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_UID: (state, user_id) => {
      state.user_id = user_id
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
    },
    SET_menuList: (state, menuList) => {
      setCookies('menuList', menuList)
      state.menuList = menuList
    },
    SET_roleSelf: (state, roleSelf) => {
      setCookies(ROLESELF, roleSelf)
      state.roleSelf = roleSelf
    },
    SET_ISBACKCHOOSE: (state, choose) => {
      setCookies(ISBACKCHOOSE, choose)
      state.isBackChoose = choose
    },
    SET_BUSINESS: (state, business) => {
      setCookies(BUSINESS, business.business)
      setCookies(ROLELEVEL, business.roleLevel)
      setCookies(businessName, business.businessName)
      state.business = business.business
      state.roleLevel = business.roleLevel
      state.businessName = business.businessName
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login({ username: username, password: userInfo.password }).then(response => {
          const data = response.data
          setCookies(UIDKey, data.id)
          setCookies(TokenKey, data.token)
          setCookies(NICKNAMEKey, data.username)
          setVcode(data.vcode)
          commit('SET_TOKEN', data.token)
          commit('SET_UID', data.id)
          commit('SET_NICKNAME', data.username)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getPreset(state.token).then(response => {
          const data = response.data
          if (data.others.length > 0 || data.base_event.length > 0) { // 验证返回的roles是否是一个非空数组
            // debugger
            const menuList = [...data.others]
            // debugger
            if (data.base_event.length > 0) {
              menuList.push('base_event')
            }
            const menu = [... new Set(menuList)]
            commit('SET_menuList', menu)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      commit('SET_TOKEN', '')
      commit('SET_UID', '')
      commit('SET_EMAIL', '')
      commit('SET_NICKNAME', '')
      commit('SET_menuList', '')
      removeCookies(UIDKey)
      removeCookies(TokenKey)
      removeCookies(NICKNAMEKey)
      removeCookies(EMAILKey)
      removeCookies('menuList')
      localStorage.clear()
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        // removeToken()
        resolve()
      })
    }
  }
}

export default user
